package com.gxgeek.hello;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties;

/**
 * @author gaoxing
 * @create 2018-05-16 上午12:36
 */
public class Producer {

    private final static String QUEUE_NAME = "hello";

    public static void main(String[] args) throws Exception {
        //步骤一：建立连接
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        factory.setPort(5672);
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        //步骤二：定义消息队列
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        String message = "[Message Producer] 66666";
        //步骤三：发布消息
//        channel.basicPublish("", QUEUE_NAME, MessageProperties.TEXT_PLAIN, message.getBytes("UTF-8"));
        channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8"));
        System.out.println("[MessageQueue] Message sent => message=" + message);
        //关闭连接
        channel.close();
        connection.close();
    }


}
